<template>
	<view class="container">
		<view class="page-banner">
			<view class="grid-body">
				<view class="flex info items-center">
					<view class="type">{{currentExam.typeLabel}}</view>
					<view>({{currentExam.score}}分)</view>
				</view>
				<view class="content">{{ currentExam.content }}</view>

				<view v-for="(item, index) in currentExam.optionList" :key="index" class="option"
					:class="{ active: isCorrect(item), error: isError(item) }">
					<text v-if="currentExam.type==='3'">{{ item.content }}</text>
					<text v-else>{{ item.options }}.{{ item.content }}</text>
					<image v-if="isCorrect(item)" src="/static/images/online-examination/select.png"></image>
					<image v-if="isError(item)" src="/static/images/online-examination/error.png"></image>
				</view>
				<view v-if="currentExam.type==='3'" class="answer flex-space-between">
					<view>正确答案：<text
							class="correct">{{ getJudmentOptions(currentExam.correctOptionIds) || '--' }}</text>
					</view>
					<view>已选答案：<text class="error">{{ getJudmentOptions(currentExam.selectOptionIds) || '--' }}</text></view>
				</view>
				<view v-else class="answer flex-space-between">
					<view>正确答案：<text class="correct">{{ getOptions(currentExam.correctOptionIds) || '--' }}</text>
					</view>
					<view>已选答案：<text class="error">{{ getOptions(currentExam.selectOptionIds) || '--' }}</text></view>
				</view>
				<view class="analys">
					解析：{{currentExam.analysis || '--'}}
				</view>

			</view>
		</view>
		<view class="page-footer flex-space-between">
			<view class="answer-sheet" @click="handlePopup">
				<image src="/static/images/online-examination/answer-sheet.png"></image>
				答题卡
			</view>
			<view class="btn-group">
				<view class="big-btn preview" @click="handlePreview">上一题</view>
				<view class="big-btn next" @click="handleNext">下一题</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getSimulationTestDetail
	} from '@/api/educational-training/simulation-test.js'
	import {
		get,
		includes
	} from 'lodash'
	import {
		mapMutations,
		mapState,
		mapActions
	} from 'vuex'
	import {
		reqGetDetail
	} from '@/api/educational-training/online-examination'

	export default {
		name: 'examination-detail',

		props: {},

		data() {
			return {
				id: '',
				testPaperData: {},
				paperList: []
			}
		},

		computed: {
			...mapState({
				currentPaperIndex: (state) => state.onlineExam.currentPaperIndex
			}),
			currentExam() {
				return this.paperList[this.currentPaperIndex] || {}
			},
			// paperList() {
			// 	return [
			// 		...get(this.testPaperData, 'singleList', []),
			// 		...get(this.testPaperData, 'multipleList', []),
			// 		...get(this.testPaperData, 'judgementList', [])
			// 	]
			// },
			paperTotal() {
				return get(this.paperList, 'length', 0)
			},
			percent() {
				return Math.floor(this.currentPaperIndex / this.paperTotal) * 100
			}
		},

		watch: {},

		created() {},

		onLoad({
			id
		}) {
			this.id = id
			this.clearStore()
			this.initData()
		},

		destroyed() {},

		methods: {
			...mapMutations(['setCurrentPaperIndex', 'clearStore']),
			async initData() {
				const res = await getSimulationTestDetail(this.id)
				this.testPaperData = res.data
				uni.setNavigationBarTitle({
					title: this.testPaperData.planName
				})
				for (let i of this.testPaperData.questionTypes) {

					for (let j of i.questionList) {
						let correctIds = []
						j.type = i.questionType
						j.typeLabel = i.questionTypeLabel
						j.score = i.score
						for (let p of j.optionList) {
							if (p.rightFlag === '1') {
								correctIds.push(p.id)
							}
						}
						j.correctOptionIds = correctIds
						this.paperList.push(j)
					}
				}
			},
			handlePreview() {
				if (this.currentPaperIndex > 0) {
					this.setCurrentPaperIndex(this.currentPaperIndex - 1)
				}
			},
			handleNext() {
				if (this.currentPaperIndex + 1 < this.paperTotal) {
					this.setCurrentPaperIndex(this.currentPaperIndex + 1)
				}
			},
			isCorrect({
				id
			}) {
				return includes(this.currentExam.correctOptionIds, id)
			},
			isError({
				id
			}) {
				return !this.isCorrect({
					id
				}) && includes(this.currentExam.selectOptionIds, id)
			},
			getJudmentOptions(list = []) {
				console.log('list', list)
				console.log('this.currentExam.optionList', this.currentExam.optionList)
				let arr = list.map(
					id => get(this.currentExam.optionList.find(item => item.id === id), 'content')
				)
				arr.sort()
				return arr.join()
			},
			getOptions(list = []) {
				console.log('currentExam', this.currentExam)
				console.log('list', list)
				console.log('this.currentExam.optionList', this.currentExam.optionList)
				let arr = list.map(
					id => get(this.currentExam.optionList.find(item => item.id === id), 'options')
				)
				arr.sort()
				return arr.join()
			},
			handlePopup() {
				uni.navigateTo({
					url: `/pages/work/mock-test/mock-test-answer-sheet-view?id=${this.id}&mode=view`
				})
			},
		}
	}
</script>

<style>
	page {
		height: 100%;
	}
</style>
<style scoped lang="scss" type="text/scss">
	.container {
  position: relative;
  height: 100%;
  background-color: #88b4c4;
  overflow-y: auto;
  padding-bottom: 180rpx;
}
.page-banner {
  padding: 0 30rpx;
  .grid-body {
    margin: 30rpx 0 0;
    padding: 30rpx;
    color: #333333;

    .type {
      width: 100rpx;
      height: 50rpx;
      line-height: 50rpx;
      background: #88b4c4;
      border-radius: 10rpx 10rpx 10rpx 10rpx;
      color: #fff;
      font-size: 12px;
      text-align: center;
    }

    .content {
      margin-top: 30rpx;
      margin-bottom: 40rpx;
      font-size: 30rpx;
      font-weight: bold;
      word-break: break-all;
    }

    .option {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding:30rpx 30rpx 30rpx 20rpx;
      width: 100%;
      //height: 88rpx;
      background: #f5f5f5;
      border-radius: 10rpx 10rpx 10rpx 10rpx;
      margin-bottom: 20rpx;
      font-size: 30rpx;

      & text{
        display: inline-block;
        min-height: 48rpx;
        line-height: 48rpx;
		word-break: break-all;
      }

      &.active {
        background: #ebf2f5;
      }
      &.error {
        background: #efdedb;
      }
      image {
        flex-shrink: 0;
        width: 48rpx;
        height: 48rpx;
      }
    }
    .btn-group {
      margin-top: 40rpx;
    }
  }
}
.page-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 158rpx;
  background-color: #fff;
  padding-right: 30rpx;
  padding-left: 46rpx;

  .answer-sheet image {
    display: block;
    margin: 0 auto 5rpx;
    width: 54rpx;
    height: 54rpx;
  }
  .preview {
    width: 231rpx;
    color: #88b4c4;
    background: #fff;
  }
  .next {
    margin-left: 30rpx;
    width: 231rpx;
    color: #fff;
    background: #88b4c4;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;

  .big-btn {
    border-radius: 44rpx 44rpx 44rpx 44rpx;
    font-size: 34rpx;
    text-align: center;
    height: 88rpx;
    line-height: 88rpx;
  }
}
.answer {
  font-size: 30rpx;
  .correct {
    font-size: 40rpx;
    color: #88B4C4;
    font-weight: bold;
  }
  .error {
    font-size: 40rpx;
    color: #A14430;
    font-weight: bold;
  }
}
.analys {
  color: #999999;
  font-size: 14px;
  margin-top: 20rpx;
  word-break: break-all;
}
</style>
